package com.heima.wemedia.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.dto.WmNewsDto;
import com.heima.model.common.wemedia.dto.WmNewsPageReqDto;
import com.heima.model.common.wemedia.entity.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 自媒体文章表(WmNews)表控制层
 *
 * @author makejava
 * @since 2022-04-12 15:40:39
 */
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {
@Autowired
private WmNewsService wmNewsService;


    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody WmNewsDto wmNewsDto){

        return wmNewsService.submit(wmNewsDto);
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.list(dto);
    }

    @GetMapping("/one/{newsId}")
    public ResponseResult update(@PathVariable("newsId") Long newsId) {

       return ResponseResult.okResult(wmNewsService.queryById(newsId)) ;

    }


    @GetMapping("/del_news/{id}")
    public ResponseResult delete(@PathVariable("id") Long id){

        return ResponseResult.okResult(wmNewsService.deleteById(id));
    }




    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseResult deleteById(Long id) {
        return ResponseResult.okResult(this.wmNewsService.deleteById(id));
    }




    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<WmNews> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.wmNewsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmNews 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<WmNews> add(WmNews wmNews) {
        return ResponseEntity.ok(this.wmNewsService.insert(wmNews));
    }

    /**
     * 编辑数据
     *
     * @param wmNews 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<WmNews> edit(WmNews wmNews) {
        return ResponseEntity.ok(this.wmNewsService.update(wmNews));
    }



}

